//
//  TCollectionViewControllelr.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/8.
//  Copyright © 2019 李文强. All rights reserved.
//

#import "TCollectionViewControllelr.h"
#import "SYRequestAPI+Teach.h"
#define CellSP 20*fw
#define CellW 50*fw

#import "TWorkListVC.h"
#import "KeChengNectVC.h"
#import "TeacherClassListModel.h"

@interface TCollectionViewControllelr ()<TeachTopSelectedDelegate,TWorkSelectedTopViewDelegate,TestSelectedTopViewDelegate>
{
    BOOL Type;
    NSString *tableName;
    int viewType;
    TWorkListVC *workListView;
    NSString * cellType;
    CJClassSelectedView *classView;
    TeacherClassListModel *mainClassModel;
}
//课程点击cell的下级界面
@property (nonatomic, strong) KeChengNectVC *keChengListContendView;
//教师班级数组
@property (nonatomic, strong) NSMutableArray *classDataArray;
@end

@implementation TCollectionViewControllelr

static NSString * const reuseIdentifier = @"Cell";
- (TeachTopSelectedView *)topSelectView{
    if (!_topSelectView) {
        TeachTopSelectedView *topView = [[TeachTopSelectedView alloc]init];
        topView.frame = CGRectMake(0, 0, 875*fw, 44);
        topView.delegate = self;
        _topSelectView = topView;
    }
    return _topSelectView;
}
- (TWorkSelectedTopView *)topWorkView{
    if (!_topWorkView) {
        TWorkSelectedTopView *topView = [[TWorkSelectedTopView alloc]init];
        topView.frame = CGRectMake(0, 0, 875*fw, 44);
        topView.delegate = self;
        _topWorkView = topView;
    }
    return _topWorkView;
}
- (TestTopNavView *)testTopView{
    if (!_testTopView) {
        TestTopNavView *topView = [[TestTopNavView alloc]init];
        topView.frame = CGRectMake(0, 0, 875*fw, 44);
        topView.delegate = self;
        _testTopView = topView;
    }
    return _testTopView;
}
- (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout *)layout andControllViewType:(ControllViewType)type{
    if ([super initWithCollectionViewLayout:layout]) {
        Type = YES;
        viewType = type;
        self.view.backgroundColor = MAINBACKCOLOR;
        [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
        [self.collectionView registerClass:[TCollectionViewCell class] forCellWithReuseIdentifier:@"TCollectionViewCell"];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeView:) name:@"ChangeCellSpaNotice" object:nil];
        // Do any additional setup after loading the view.
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.top.equalTo(@44);
            make.right.equalTo(@0);
            make.bottom.equalTo(@0).offset(-20);
        }];
        
        if (type == 0) {
            self.collectionView.backgroundColor = [UIColor whiteColor];
            [self.view addSubview:self.topSelectView];
            [self.topSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(@0);
                make.right.equalTo(@0);
                make.top.equalTo(@0);
                make.height.equalTo(@44);
            }];
            [self getWeiShangKeShiRequest:@""];
        }else if (type == 1){
            self.collectionView.backgroundColor = [UIColor whiteColor];
            [self.view addSubview:self.topWorkView];
            [self.topWorkView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(@0);
                make.right.equalTo(@0);
                make.top.equalTo(@0);
                make.height.equalTo(@44);
            }];
        }else if (type == 2){
            self.collectionView.backgroundColor = [UIColor whiteColor];
            [self.view addSubview:self.testTopView];
            [_testTopView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(@0);
                make.right.equalTo(@0);
                make.top.equalTo(@0);
                make.height.equalTo(@44);
            }];
        }else if (type == 3){
            self.collectionView.backgroundColor = [UIColor whiteColor];
        }
        mainClassModel = [TeacherClassListModel new];
        
    }
    
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    _classDataArray = [NSMutableArray new];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showView) name:@"ShowView" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(weiShangKeShi:) name:@"WeiShangKeShiNotice" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(yiShangKeShi:) name:@"YiShangKeShiNotice" object:nil];
}
- (void)weiShangKeShi:(NSNotification *)info{
    cellType = @"未上课时";
    [self.collectionView reloadData];
}
- (void)yiShangKeShi:(NSNotification *)info{
    cellType = @"已上课时";
    [self.collectionView reloadData];
}
- (void)showView{
    self.collectionView.hidden = NO;
    self.topWorkView.hidden = NO;
}
- (void)changeView:(NSNotification *)info{
    BOOL type = [[info.userInfo objectForKey:@"Type"] intValue];
    if (type == YES) {
        Type = NO;
    }else{
        Type = YES;
    }
    [self.collectionView reloadData];
}
- (void)leftButtonClick{
    [CJYearMonthSelectedView showDatePickerWithTitle:@"选择月份" minDateStr:@"2017-10" resultBlock:^(NSString *selectValue) {
        //选择完成后的操作
        NSLog(@"selected month is %@", selectValue);
    }];
}
#pragma mark - 课程未上课时
- (void)rightButtonClick{
    __block TCollectionViewControllelr *mySelf = self;
    [CJClassSelectedView showDatePickerWithTitle:@"选择班级" minDateStr:@"年级一" resultBlock:^(TeacherClassListModel * _Nonnull selectValue) {
        NSLog(@"输出选择的班级 %@",selectValue);
        self->mainClassModel = selectValue;
        [mySelf getWeiShangKeShiRequest:@""];
    }];
}
- (void)leftWorkButtonClick{
    [CJYearMonthSelectedView showDatePickerWithTitle:@"选择月份" minDateStr:@"2017-10" resultBlock:^(NSString *selectValue) {
        //选择完成后的操作
        NSLog(@"selected month is %@", selectValue);
    }];
}
- (void)rightWorkButtonClick{
    [CJClassSelectedView showDatePickerWithTitle:@"选择班级" minDateStr:@"年级一" resultBlock:^(TeacherClassListModel * _Nonnull selectValue) {

    }];
}
- (void)leftTestButtonClick{
    [CJYearMonthSelectedView showDatePickerWithTitle:@"选择月份" minDateStr:@"2017-10" resultBlock:^(NSString *selectValue) {
        //选择完成后的操作
        NSLog(@"selected month is %@", selectValue);
    }];
}
#pragma mark - 弹出选择班级的alert
- (void)rightTestButtonClick{
    [CJClassSelectedView showDatePickerWithTitle:@"选择班级" minDateStr:@"11" resultBlock:^(TeacherClassListModel * _Nonnull selectValue) {
        NSLog(@"选中的模型 %@",selectValue);
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {

    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 20;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"TCollectionViewCell" forIndexPath:indexPath];
    cell.titleLabel.text = @"慧阅读2-2C Lesson 25";
    if ([cellType isEqualToString:@"已上课时"]) {
        cell.chuQingLabel.hidden = NO;
    }else if ([cellType isEqualToString:@"未上课时"]){
        cell.chuQingLabel.hidden = YES;
    }else{
        cell.chuQingLabel.hidden = YES;
    }
        
    return cell;
}
#pragma mark == collectionViewDelegate
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(204*fw, 105*fh);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
//top, left, bottom, right
    if (Type == YES) {
        return UIEdgeInsetsMake(CellSP, CellSP, CellSP,CellSP);
    }else{
        return UIEdgeInsetsMake(CellSP, CellW, CellSP,CellW);
    }
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return CellSP;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return CellSP;
}

//是否允许移动Item
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0)
{
    return YES;
}
//移动Item时触发的方法
- (void)collectionView:(UICollectionView *)collectionView moveItemAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath NS_AVAILABLE_IOS(9_0);
{
    
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//    [self.mDelegate collectionViewCellClick:indexPath];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    switch (viewType) {
        case 0:
        {
            //课程
            _keChengListContendView = [[KeChengNectVC alloc]init];
            _keChengListContendView.view.backgroundColor = [UIColor whiteColor];
            [self.view addSubview:_keChengListContendView.view];
            _keChengListContendView.view.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, APP_HEIGHT);
            [UIView animateWithDuration:0.5 animations:^{
                self->_keChengListContendView.view.frame = CGRectMake(0, 0, self.view.frame.size.width, APP_HEIGHT);
            } completion:^(BOOL finished) {
                [MBProgressHUD hideHUDForView:self.view animated:YES];
            }];
        }
            break;
        case 1:
        {
            //作业
            workListView = [[TWorkListVC  alloc]init];
            [self.view addSubview:workListView.view];
            workListView.view.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
            [UIView animateWithDuration:0.5 animations:^{
               self->workListView.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
            } completion:^(BOOL finished) {
                self.collectionView.hidden = YES;
                self.topWorkView.hidden = YES;
                [MBProgressHUD hideHUDForView:self.view animated:YES];
            }];
        }
            break;
        case 2:
        {
            //测试
        }
            break;
        case 3:
        {
            //班级
        }
            break;
            
        default:
            break;
    }
    [PublickFunctionObject changeMainView:@"TeachChangeNotice" AndType:Type];
    
}
#pragma mark <UICollectionViewDelegate>

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}


// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
- (void)getDataForView:(NSString *)name{
    tableName = name;
    [self.collectionView reloadData];
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"ShowView" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"WeiShangKeShiNotice" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"YiShangKeShiNotice" object:nil];
}

#pragma mark - 课程-未上课时
- (void)getWeiShangKeShiRequest:(NSString *)date{
    if (date.length == 0) {
        date = @"";
    }
    if (mainClassModel.id.length == 0) {
        mainClassModel.id = @"";
    }
    NSDictionary *parama = @{@"classId":mainClassModel.id,
                             @"date":date};
    [MBProgressHUD mj_showLoading];
    [SYRequestAPI sy_getUnAttend:parama success:^(id  _Nonnull responseObject) {
        NSLog(@"得到课程-未上课时数据 %@",responseObject);
        [MBProgressHUD mj_hideHUD];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUD];
    }];
}
#pragma mark - 课程-已上课时
@end
